//
//  ViewController.swift
//  iOS_weex
//
//  Created by 李浩 on 2017/5/10.
//  Copyright © 2017年 李浩. All rights reserved.
//

import UIKit
import WeexSDK

class ViewController: UIViewController {
    
    var instance: WXSDKInstance!
    var weexView: UIView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        instance = WXSDKInstance.init()
        instance.viewController = self
        instance.frame = self.view.frame

        instance.onCreate = {[weak self] view in
            self?.weexView?.removeFromSuperview()
            self?.weexView = view
            self?.view.addSubview((self?.weexView)!)
            UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, self?.weexView)
        }
        
        instance.onFailed = {error in
            
        }
        
        instance.renderFinish = {view in
            print("--------------------")
        }

        let url = Bundle.main.url(forResource: "honour_peo.weex", withExtension: "js")
        instance.render(with: url, options: ["bundleUrl": url?.absoluteString ?? ""], data: nil)
    }
    
    func downloadImageWithURL(url: String, imgFrame: CGRect, userInfo: [String: String], completedBlock: ()-> Void) -> WXImageOperationProtocol {
        var imgUrl = url
        if (url.hasPrefix("//")) {
            imgUrl = "http:" + url
        }
        return SDWebImageManager.shared().loadImage(with: URL(string: imgUrl), options: SDWebImageOptions(rawValue: 0), progress: { (receivedSize, expectedSize, url) in
            
           }) { (image, data, error, cacheType, finished, imageURL) in
            
        } as! WXImageOperationProtocol
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    deinit {
        instance.destroy()
    }
}

